#!/bin/bash

if [[ $1 == "lova-pre" ]];then
if [[ $2 == "hotfix" ]];then
for qcservice in `kubectl get deployments -n lova-qc -o json --kubeconfig=/root/.kube/config-DQ|jq -r '.items[].metadata.name'|grep -v spring-cloud|grep -v tms-agent|grep -v '^ms'`
do
   qcimagename=`kubectl get deployments $qcservice -n lova-qc -o json --kubeconfig=/root/.kube/config-DQ|jq -r '.spec.template.spec.containers[].image'|awk -F/ '{print $NF}'|awk -F: '{print ($1)}'`
   qcver=`kubectl get deployments $qcservice -n lova-qc -o json --kubeconfig=/root/.kube/config-DQ|jq -r '.spec.template.spec.containers[].image'|awk -F: '{print $NF}'`
   for preservice in `kubectl get deployments -n tls-pre -o json --kubeconfig=/root/.kube/config-y3pre|jq -r '.items[].metadata.name'|grep -v spring-cloud`
   do
     preimagename=`kubectl get deployments $preservice -n tls-pre -o json --kubeconfig=/root/.kube/config-y3pre|jq -r '.spec.template.spec.containers[].image'|awk -F/ '{print $NF}'|awk -F: '{print ($1)}'`
     prever=`kubectl get deployments $preservice -n tls-pre -o json --kubeconfig=/root/.kube/config-y3pre|jq -r '.spec.template.spec.containers[].image'|awk -F: '{print $NF}'`
     if [[ $qcservice == $preservice ]];then
       if [[ ${qcimagename}:${qcver} == ${preimagename}:${prever} ]];then
          #echo "${qcimagename}:${qcver} equal ${preimagename}:${prever}"
          sleep 0.1
       else
          echo "${qcimagename}:${qcver} not equal ${preimagename}:${prever}"
       fi
     fi
   done
done
else
for testservice in `kubectl get deployments -n lova-test -o json --kubeconfig=/root/.kube/config-DQ|jq -r '.items[].metadata.name'|grep -v spring-cloud|grep -v tms-agent|grep -v '^ms'`
do
   testimagename=`kubectl get deployments $testservice -n lova-test -o json --kubeconfig=/root/.kube/config-DQ|jq -r '.spec.template.spec.containers[].image'|awk -F/ '{print $NF}'|awk -F: '{print ($1)}'`
   testver=`kubectl get deployments $testservice -n lova-test -o json --kubeconfig=/root/.kube/config-DQ|jq -r '.spec.template.spec.containers[].image'|awk -F: '{print $NF}'`
   for preservice in `kubectl get deployments -n tls-pre -o json --kubeconfig=/root/.kube/config-y3pre|jq -r '.items[].metadata.name'|grep -v spring-cloud`
   do
     preimagename=`kubectl get deployments $preservice -n tls-pre -o json --kubeconfig=/root/.kube/config-y3pre|jq -r '.spec.template.spec.containers[].image'|awk -F/ '{print $NF}'|awk -F: '{print ($1)}'`
     prever=`kubectl get deployments $preservice -n tls-pre -o json --kubeconfig=/root/.kube/config-y3pre|jq -r '.spec.template.spec.containers[].image'|awk -F: '{print $NF}'`
     if [[ $testservice == $preservice ]];then
       if [[ ${testimagename}:${qcver} == ${preimagename}:${prever} ]];then
          #echo "${testimagename}:${testver} equal ${preimagename}:${prever}"
          sleep 0.1
       else
          echo "${testimagename}:${testver} not equal ${preimagename}:${prever}"
       fi
     fi
   done
done
fi
elif [[ $1 == "lova-prod" ]];then
for preservice in `kubectl get deployments -n tls-pre -o json --kubeconfig=/root/.kube/config-y3pre|jq -r '.items[].metadata.name'|grep -v spring-cloud`
do
   preimagename=`kubectl get deployments $preservice -n tls-pre -o json --kubeconfig=/root/.kube/config-y3pre|jq -r '.spec.template.spec.containers[].image'|awk -F/ '{print $NF}'|awk -F: '{print ($1)}'`
   prever=`kubectl get deployments $preservice -n tls-pre -o json --kubeconfig=/root/.kube/config-y3pre|jq -r '.spec.template.spec.containers[].image'|awk -F: '{print $NF}'`
   for prodservice in `kubectl get deployments -n tls-prod -o json --kubeconfig=/root/.kube/config-vspprod|jq -r '.items[].metadata.name'|grep -v spring-cloud`
   do
     prodimagename=`kubectl get deployments $prodservice -n tls-prod -o json --kubeconfig=/root/.kube/config-vspprod|jq -r '.spec.template.spec.containers[].image'|awk -F/ '{print $NF}'|awk -F: '{print ($1)}'`
     prodver=`kubectl get deployments $prodservice -n tls-prod -o json --kubeconfig=/root/.kube/config-vspprod|jq -r '.spec.template.spec.containers[].image'|awk -F: '{print $NF}'`
     if [[ $preservice == $prodservice ]];then
       if [[ ${preimagename}:${prever} == ${prodimagename}:${prodver} ]];then
          #echo "${preimagename}:${prever} equal ${prodimagename}:${prodver}"
          sleep 0.1
       else
          echo "${preimagename}:${prever} not equal ${prodimagename}:${prodver}"
       fi
     fi
   done
done
elif [[ $1 == "uat" ]];then
 echo "Please enter the correct parameters:lova-pre,lova-prod,lova-trial"
 exit 1
fi
